const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl,
    _index: -1,
    id: '',

    flag: true,
    page: 1,
    pagesize: 10,
    uid: '',
    list: []
  },
  onLoad: function (options) {
    this.setData({
      uid: options.id,
    })
  },
  onShow() {
    this.setData({
      page: 1,
      flag: true,
      list: []
    })
    this.getuseraddrlist()
  },

  onReachBottom() {
    this.getuseraddrlist()
  },

  getuseraddrlist() {
    let data = {
      page: this.data.page,
      pagesize: this.data.pagesize,
      uid: this.data.uid
    }
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false
    })
    getApp().globalData.gets('/api/ouyoung/user/getuseraddrlist', data).then(res => {
      wx.stopPullDownRefresh()
      if (res.data.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let arr = this.data.list.concat(res.data.data.data);
        this.setData({
          list: arr,
          page: this.data.page + 1,
          flag: true
        })
      }
    })
  },

  enter(e) {
    this.setData({
      _index: e.currentTarget.dataset.index,
      id: e.currentTarget.dataset.id
    })
  },
  confirm() {
    if (this.data.id == '') {
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      })
    } else {
      wx.setStorageSync('stote_user_address', this.data.list[this.data._index])
      wx.navigateBack({
        delta: 1
      })
    }
  },
  toaddAddress() {
    wx.navigateTo({
      url: '../matron/addAddress?user_id=' + this.data.uid,
    })
  },
  edit_address(e) {
    let add_type = e.currentTarget.dataset.add_type
    if (add_type == 1) {
      getApp().showModal('该地址为用户私人地址，只能本人修改')
    } else {
      wx.navigateTo({
        url: '../matron/addAddress?id=' + e.currentTarget.dataset.id + "&user_id=" + this.data.uid
      })
    }
  },
})